package com.qckj.amz.controller;

import com.qckj.amz.domain.ProductTempRlat;
import com.qckj.amz.service.IProductTempRlatService;
import com.qckj.common.annotation.Log;
import com.qckj.common.core.controller.BaseController;
import com.qckj.common.core.domain.AjaxResult;
import com.qckj.common.core.page.TableDataInfo;
import com.qckj.common.enums.BusinessType;
import com.qckj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 产品关系Controller
 *
 * @author luchuan
 * @since 1.0 2021-12-24
 */
@RestController
@RequestMapping("/amz/productTempRlat")
public class ProductTempRlatController extends BaseController {
    @Autowired
    private IProductTempRlatService productTempRlatService;

    /**
     * 查询产品关系列表
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempRlat:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductTempRlat productTempRlat) {
        Page<ProductTempRlat> page = productTempRlatService.findProductTempRlatPage(productTempRlat);
        return getDataTable(page);
    }

    /**
     * 导出产品关系列表
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempRlat:export')")
    @Log(title = "产品关系", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProductTempRlat productTempRlat) {
        List<ProductTempRlat> list = productTempRlatService.findProductTempRlatList(productTempRlat);
        ExcelUtil<ProductTempRlat> util = new ExcelUtil<>(ProductTempRlat.class);
        return util.exportExcel(list, "productTempRlat");
    }

    /**
     * 获取产品关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempRlat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productTempRlatService.findById(id));
    }

    /**
     * 新增产品关系
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempRlat:add')")
    @Log(title = "产品关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductTempRlat productTempRlat) {
        productTempRlatService.save(productTempRlat);
        return AjaxResult.success();
    }

    /**
     * 修改产品关系
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempRlat:edit')")
    @Log(title = "产品关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductTempRlat productTempRlat) {
        productTempRlatService.save(productTempRlat);
        return AjaxResult.success();
    }

    /**
     * 删除产品关系
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempRlat:remove')")
    @Log(title = "产品关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        productTempRlatService.deleteByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }


    /**
     * 通过产品代码获取变体集合
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempRlat:query')")
    @RequestMapping(value = "/getListByProductCode")
    public AjaxResult getListByProductCode(@RequestBody String productCode) {
        AjaxResult res = productTempRlatService.getListByProductCode(productCode);
        return res;
    }

    /**
     * 创建产品关系
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempRlat:add')")
    @PostMapping("/createRlat")
    public AjaxResult createRlat(@RequestParam(name = "productCode") String productCode,
                                 @RequestParam(name = "variationTheme") String variationTheme,
                                 @RequestParam(name = "colorName") List<String> colorName,
                                 @RequestParam(name = "sizeName") List<String> sizeName) {
        AjaxResult res = productTempRlatService.createRlat(productCode,variationTheme,colorName,sizeName);
        return res;
    }

    /**
     * 保存当前新增和修改的变体记录
     * @param addAndUpdateRlatMap  当前产品代码的所有新增和修改，前端封存参数如下
     *           var formData = new FormData();
     *           formData.append("addRlatList",JSON.stringify(insertRecords));
     *           formData.append("updateRlatList",JSON.stringify(updateRecords));
     * @param productCode           产品代码
     * @return
     */
    @RequestMapping(value = "/updateRowsRlat", method = RequestMethod.POST)
    public AjaxResult updateRowsRlat(@RequestBody Map<String,List<ProductTempRlat>> addAndUpdateRlatMap, @RequestParam("productCode") String productCode) {
        AjaxResult res = productTempRlatService.updateRowsRlat(addAndUpdateRlatMap,productCode);
        return res;
    }


    /**
     * 删除当前修改变体记录
     * @param rlatList
     * @return
     */
    @PostMapping("/deleteRowsRlat")
    public AjaxResult deleteRowsRlat(@RequestBody List<Map<String,Object>> rlatList) {
        AjaxResult res = productTempRlatService.deleteRowsRlat(rlatList);
        return res;
    }

    /**
     *
     * @param productCode
     * @param idKey
     * @param eventSign     点击事件标记：1清空主图；2清空附图；3删除主图-单个；4删除附图-单个
     * @param imageUrl
     * @return
     */
    @PostMapping("/deleteImageUrl")
    public AjaxResult deleteImageUrl(@RequestParam(name = "productCode") String productCode,
                                     @RequestParam(name = "idKey") String idKey,
                                     @RequestParam(name = "eventSign") Integer eventSign,
                                     @RequestParam(name = "imageUrl") String imageUrl) {
        AjaxResult res = productTempRlatService.deleteImageUrl(productCode,idKey,eventSign,imageUrl);
        return res;
    }

    /**
     * 上传图片处理
     *
     * @param productCode   产品代码
     * @param idKey         操作选中产品的主键id
     * @param eventSign     点击事件标记：1上传主图；2上传附图；3上传主图-单个变体；4上传附图-单个变体
     * @param imgList       选中的图片数组
     * @return
     */
    @RequestMapping(value = "/handleUploadImage", method = RequestMethod.POST)
    public AjaxResult handleUploadImage(@RequestParam(name = "productCode") String productCode,
                                          @RequestParam(name = "idKey") String idKey,
                                          @RequestParam(name = "eventSign") Integer eventSign,
                                          @RequestParam(name = "imgArr") List<String> imgList) {
        AjaxResult res = productTempRlatService.handleUploadImage(productCode,idKey,eventSign,imgList);
        return res;
    }


}
